<!-- 填写维修单 -->
<template>
  <div class="content">
    <avue-form
      class="avueForm"
      v-if="visible"
      v-model="formData"
      :option="config"
      @submit="requestPutTask"
    />
  </div>
</template>

<script>
import {
  putAvueForm,
  getDefinition,
  getInstance,
  taskVariables as taskVariablesPublic,
  infoPush,
  equipment,
  serialNumber,
  delNumber
} from '@/api/index'

import { Toast, Notify } from 'vant'

export default {
  data () {
    return {
      visible: true,
      formData: {}, // 表单数据
      config: {}, // 表单配置
      taskData: {}, // 任务数据
      submitFormType: 2, // 1 自动提交 2 不自动提交，返回表单数据 3 自动创建任务
      definitionKey: '',
      businessKey: null
    }
  },

  components: {
    [Notify.name]: Notify
  },

  computed: {},

  async created () {
    const { key, map, id } = this.$route.query
    this.instance(key, map, id)
  },
  methods: {

    /**
     * 启动流程
     */
    async instance (key, map, id = null) {
      const loading = Toast.loading({
        message: '加载中...'
      })

      var res = await getDefinition({
        definitionKey: key,
        tenantId: window.localStorage.getItem('tenantId'),
        isNewest: true
      })

      if (res && res.list && res.list[0]) {
        this.definitionKey = key
        this.businessKey = id
        this.submitFormType = 3

        var taskVariables = await taskVariablesPublic({
          type: 'pc',
          processId: res.list[0].id,
          taskId: 'StartEvent',
          tenantId: window.localStorage.getItem('tenantId')
        })
        this.formData = map
        this.formData.itemNo = await serialNumber({
          type: 'repair',
          tenantId: window.localStorage.getItem('tenantId')
        })
        this.formData.device_areaName = map.device_areaInfo.name
        console.log(map, 'ppppppp')
        if (taskVariables && taskVariables.detail) {
          this.config = JSON.parse(taskVariables.detail)
          this.visible = true
        } else {
          this.requestPutTask()
        }
        loading.clear()
      } else {
        Toast.fail('请检查流程图！')
      }
    },
    putAvueForm (data) {
      putAvueForm(data).then(res => {

      })
    },
    /**
     * 任务提交
     * @param {Object} data 表单数据
     */
    async requestPutTask (data = {}) {
      switch (this.submitFormType) {
        case 1:
          try {
            await putAvueForm({
              taskId: this.taskData.id,
              variables: data,
              complete: 1
            })
            this.$emit('submitForm')
            this.close()
          } catch (error) {
            console.log(error)
          }
          break
        case 2:
          this.$emit('retFormData', data)
          break
        case 3:
          await getInstance({
            key: this.definitionKey,
            map: { ...this.formData, ...data },
            businessKey: this.businessKey,
            tenantId: window.localStorage.getItem('tenantId')
          })
          // 通知
          await infoPush({
            category: '维修类',
            contentWithReplaceHolder: '您有1条待分配的报修任务',
            tagName: '综合业务管理人员——维修任务',
            title: '维修通知'
          })
          // 修改状态
          await equipment({
            equipments: [{
              id: this.formData.device_id,
              stateId: '2'
            }]
          })
          // 删除流水号
          await delNumber({
            generate: this.formData.itemNo,
            tenantId: window.localStorage.getItem('tenantId'),
            type: 'repair'
          })
          Toast.success('操作成功')
          this.$router.back()
          break
      }
    }
  }
}
</script>
<style lang='less' scoped>
.content {
  width: 100%;
  height: 100%;
  background: #fff;
  > .avueForm {
    margin-top: 15px;
  }
}
</style>
